from tkinter import *
from tkinter.ttk import Combobox
import os
import json
from threading import Thread


def download(user, passwd, choice):
    # print(user, passwd, choice)
    if choice == '不清除缓存':
        a, b = False, False
    elif choice == '清除缓存图片':
        a, b = True, False
    elif choice == '清除缓存文字':
        a, b = False, True
    else:
        a, b = True, True
    new_config = {
        "账号": user,
        "密码": passwd,
        "是否删除图片": a,
        '是否删除文字': b
    }
    json.dump(new_config, open('config.json', 'w', encoding='gbk'), indent=4, ensure_ascii=False)
    root.destroy()
    from downloader import Dairy
    my = Dairy()
    my.run()


WIDTH, HEIGHT = 320, 180
# 创建起始画布
root = Tk()
root.title('Your Diary Downloader')
root.geometry(f'{WIDTH}x{HEIGHT}')  # 这里的乘号不是 * ，而是小写英文字母 x
root.configure(bg='#5CA9E1')
# 设置两个输入框
if os.path.exists('config.json'):
    config = json.load(open('config.json', 'r', encoding='gbk'))
    email = config['账号']
    password = config['密码']
else:
    email = None
    password = None
user = Label(root, text='用户名/邮箱:', font=('黑体', 10), bg='#5CA9E1', fg='#FFFAFA', justify='center')
user.place(relx=0.04, rely=0.11)
user_input = Entry(root, font=('TimesNewRoman', 12), justify='center', relief='sunken', insertwidth='5')
user_input.place(relx=0.35, rely=0.1, relwidth=0.6, relheight=0.13)

passwd = Label(root, text='密码:', font=('黑体', 10), bg='#5CA9E1', fg='#FFFAFA', justify='center')
passwd.place(relx=0.04, rely=0.31)
passwd_input = Entry(root, font=('TimesNewRoman', 12), justify='center', relief='sunken', insertwidth='5')
passwd_input.place(relx=0.35, rely=0.3, relwidth=0.6, relheight=0.13)

if email is not None:
    user_input.insert(index=0, string=email)
    passwd_input.insert(index=0, string=password)
# 是否清除缓存
Choice = {
    '不清除缓存': 0,
    "清除图片": 1,
    "清除文本": 2,
    "两者都清除": 3,
}
choice_var = StringVar()
choice_combobox = Combobox(root, justify='center', textvariable=choice_var, value=tuple(Choice.keys()))
choice_combobox.current(0)
choice_label = Label(root, text='选项:', font=('黑体', 10), bg='#5CA9E1', fg='#FFFAFA', justify='center')
choice_label.place(relx=0.04, rely=0.51)
choice_combobox.place(relx=0.35, rely=0.5, relwidth=0.6, relheight=0.13)
# 设置下载按钮
btn1 = Button(root, text='导出', relief='flat',
              bg='#559DCF', fg='white',
              font=('黑体', 12, 'bold'),
              command=lambda: download(user_input.get(), passwd_input.get(), choice_combobox.get()))
btn1.place(relx=0.05, rely=0.7, relwidth=0.6, relheight=0.13)

root.resizable(False, False)  # 防止用户调整尺寸
root.mainloop()
